package top.zhmq.goodsprovider.controller;


import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.zhmq.commonmodule.model.Goods;
import top.zhmq.goodsprovider.dao.GoodsDao;
import top.zhmq.goodsprovider.dao2.GoodsDao2;

import javax.naming.directory.SearchResult;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品控制层
 *
 * @author Glory
 * @classname GoodsController
 * @create 2021-06-03 10:36
 */

@RestController
public class GoodsController {
    @Autowired
    GoodsDao goodsDao;
    @Autowired
    GoodsDao2 goodsDao2;
    @Autowired
    RestHighLevelClient restHighLevelClient;

    @RequestMapping(value = "/getAllgoods", method = RequestMethod.GET)
    public List<Goods> getAllgoods() throws IOException {
        List<Goods> goods = goodsDao.getAllgoods();
        //TODO 此处存入es，已实现
        BulkRequest request = new BulkRequest();
        request.timeout("10s");
        for (int i = 0; i < goods.size(); i++) {
            request.add(new IndexRequest("shoptest").id("" + (i + 1))
                    .source(JSON.toJSONString(goods.get(i)), XContentType.JSON));
        }
        BulkResponse responses = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
        System.out.println("向es插入数据后，是否有失败的（应改为false）：" + responses.hasFailures());
        return goods;
    }

    @RequestMapping(value = "/getOnegoods", method = RequestMethod.GET)
    public List<Goods> getOnegoods(@RequestParam(value = "gname") String gname) throws IOException {
        //TODO 此处进行改写，使用ES查询！
//        List<Map<String,Object>> mapList;
//        SearchRequest searchRequest = new SearchRequest("shoptest");
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("gname",gname);
//        sourceBuilder.query(termQueryBuilder);
//        searchRequest.source(sourceBuilder);
//        SearchResponse searchResponse = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
//        ArrayList<Map<String,Object>> list = new ArrayList<>();
//        for (SearchHit hit :searchResponse.getHits().getHits()){
//            list.add(hit.getSourceAsMap());
//        }
//        return goodsDao2.findGoodssByGnameLike(gname);

        List<Goods> goods = new ArrayList<>();
        SearchRequest searchRequest = new SearchRequest("shoptest");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //精确查询
        String s = "*" + gname + "*";
        WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery("gname", s);
        sourceBuilder.query(wildcardQueryBuilder);
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println("===========================");
        for (SearchHit doc : searchResponse.getHits().getHits()) {
            Map<String, Object> map = doc.getSourceAsMap();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
            }
            Goods good = JSON.parseObject(JSON.toJSONString(map), Goods.class);
            goods.add(good);
        }
        return goods;
//        return goodsDao.getOnegoods(gname);
    }

    @RequestMapping(value = "/getOnegid", method = RequestMethod.GET)
    public Goods getOnegid(@RequestParam(value = "gid") int gid) {
        return goodsDao.getOnegid(gid);
    }
}
